package com.newlink.building.userinfo.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.View
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.newlink.building.common_base.base.NL_BaseMvpActivity
import com.newlink.building.common_base.constant.Base_RouterPath.PATH_FLS_MSG
import com.newlink.building.common_base.event.EmergencyMsgBean
import com.newlink.building.common_base.model.bean.FlsMessageData
import com.newlink.building.common_base.utils.TimezoneConvert
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.databinding.ActivityMessageCenterDetailBinding
import com.newlink.building.userinfo.mvp.Module_UserInfo_FlsMessageCenterDetailPresenter
import com.newlink.building.userinfo.mvp.contract.Module_UserInfo_FlsMessageCenterDetailContract

/**
 * @author Admin
 * @date 2022/4/11
 * @description 我的-消息中心-详情页页面
 */

@Route(path = PATH_FLS_MSG)
class Module_UserInfo_FlsMessageDetailActivityNL :
    NL_BaseMvpActivity<Module_UserInfo_FlsMessageCenterDetailContract.View, Module_UserInfo_FlsMessageCenterDetailContract.Presenter>(),
    Module_UserInfo_FlsMessageCenterDetailContract.View {

    companion object {
        /**
         * 对外开放统一入口
         *
         * @param context
         * @param data     FlsMessageData
         */
        fun startSelf(context: Context?, data: FlsMessageData) {
            val intent = Intent(context, Module_UserInfo_FlsMessageDetailActivityNL::class.java)
            intent.putExtra("data", data)
            context?.startActivity(intent)
        }

        /**
         * 对外开放统一入口
         *
         * @param activity
         * @param data     FlsMessageData
         * @param requestCode     requestCode
         */
        fun startSelf(activity: Activity?, data: FlsMessageData, requestCode: Int) {
            val intent = Intent(activity, Module_UserInfo_FlsMessageDetailActivityNL::class.java)
            intent.putExtra("data", data)
            intent.putExtra("requestCode", requestCode)
            activity?.startActivityForResult(intent, requestCode)
        }
    }

//    override fun attachLayoutRes(): Int {
//        return R.layout.activity_message_center_detail
//    }

    private lateinit var mBinding: ActivityMessageCenterDetailBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityMessageCenterDetailBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun allowFontScale() = false

    override fun initData() {
    }

    override fun initView() {
        super.initView()
        findViewById<TextView>(R.id.tv_title_bar_center).text =
            resources.getText(R.string.message_detail)

        val action = intent?.action
        if (action != null && action == "go_msg") {
//            val emergencyMsgBean: EmergencyMsgBean? =
//                intent.getSerializableExtra("emergencyBean") as EmergencyMsgBean

            val emergencyMsgBean: EmergencyMsgBean? =
                intent.getSerializableExtra("emergencyBean") as EmergencyMsgBean

            emergencyMsgBean?.let {
                fitContent(
                    emergencyMsgBean.data.title,
                    emergencyMsgBean.data.content,
                    emergencyMsgBean.data.createTime,
                    emergencyMsgBean.data.id,
                    0
                )
            }
        } else {
            val data = intent?.getSerializableExtra("data") as FlsMessageData
            val requestCode = intent?.getIntExtra("requestCode", 0)
            fitContent(data.title, data.message, data.createTime, data.id, data.readFlag)
        }
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)

    }

    fun fitContent(title: String, content: String, createTime: String, id: Long, readFlag: Int) {
        mBinding.tvTitle.text = title
        mBinding.tvDate.text =
            TimezoneConvert.convertTimeFromGMTToLocal(createTime, ignoreSecond = true)
        mBinding.tvDescribe.text = content
        if (readFlag == 0) {
            mPresenter?.setMessageReadStatus(accessToken, id)
        }

    }

    override fun createPresenter() = Module_UserInfo_FlsMessageCenterDetailPresenter()

    override fun setMessageReadStatusSuccess() {
//        setResult(1001)
    }

    override fun setMessageReadStatusFail() {
    }
}